Hi, I want to create generic count function which can count:
So is it possible to create such function which receives as argument Object(unknown type) . Dinio - Wed Dec 16 04:09:32 EST 2015 |
Re: Create generic count function? The problem here is, that attributes and objects are both part of the module, so you need to be able to specify which one you want. Of course you can create several count functions with different parameters: int countObjects(Module) // counts attributes int countAttributes(Module) // ... etc. So now you want to have this nice syntax that all functions return the same. You can choose between something like that: "Literal syntax" (more DXL like): count attributes in mod; count objects in mod; ... You can also do something like this: int var = (count mod) -> attributes Or any other syntax you want. It can be realized with DXL, which one should it be? Regards, Mathias |
Re: Create generic count function? Mathias Mamsch - Wed Dec 16 05:33:58 EST 2015 The problem here is, that attributes and objects are both part of the module, so you need to be able to specify which one you want. Of course you can create several count functions with different parameters: int countObjects(Module) // counts attributes int countAttributes(Module) // ... etc. So now you want to have this nice syntax that all functions return the same. You can choose between something like that: "Literal syntax" (more DXL like): count attributes in mod; count objects in mod; ... You can also do something like this: int var = (count mod) -> attributes Or any other syntax you want. It can be realized with DXL, which one should it be? Regards, Mathias I want for example like this:.But can not make it generic.
|
Re: Create generic count function? Dinio - Wed Dec 16 06:03:01 EST 2015 I want for example like this:.But can not make it generic.
DXL is not Java. There are no generics here. There is no "for ... in ..." loop that will allow you to handle more than one type. The only way to achieve that with DXL is using eval_, i.e. generating DXL code on the fly, e.g.
int countGeneric (string childType, string parentType, _x parentVar){
int parentAddr = ((addr_ parentVar) int)
string sCode = "
int count = 0;
" childType " child = null;
" parentType " parent = addr_ " parentAddr "
for child in parent do count++;
// return the count to the eval ...
"
eval_ sCode // pray it doesn't crash
}
You should save yourself the headache and make several functions for each type and add some syntactical sugar later to emulate a function that can handle all types. Still you need to define how you want to call the function counting the attributes and the objects of a module. Even if you would have generic functions, then you still would have to tell the function which one you want, since the parameter will be a module in both cases. Regards, Mathias
|
Re: Create generic count function? Mathias Mamsch - Wed Dec 16 06:17:16 EST 2015 DXL is not Java. There are no generics here. There is no "for ... in ..." loop that will allow you to handle more than one type. The only way to achieve that with DXL is using eval_, i.e. generating DXL code on the fly, e.g.
int countGeneric (string childType, string parentType, _x parentVar){
int parentAddr = ((addr_ parentVar) int)
string sCode = "
int count = 0;
" childType " child = null;
" parentType " parent = addr_ " parentAddr "
for child in parent do count++;
// return the count to the eval ...
"
eval_ sCode // pray it doesn't crash
}
You should save yourself the headache and make several functions for each type and add some syntactical sugar later to emulate a function that can handle all types. Still you need to define how you want to call the function counting the attributes and the objects of a module. Even if you would have generic functions, then you still would have to tell the function which one you want, since the parameter will be a module in both cases. Regards, Mathias
I was wondering if we pass Object as argument on the function then can we check if this object is Module like:
is this possible in DXL? |
Re: Create generic count function? Dinio - Wed Dec 16 09:31:58 EST 2015 I was wondering if we pass Object as argument on the function then can we check if this object is Module like:
is this possible in DXL? No. But you can have achieve the same thing in DXL (have one function that you can pass everything into). You just need some boilerplate to get it done. Regards, Mathias |